﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace PlanningGIS.ArcGIS.Custom
{
    /// <summary>
    /// sde连接对象
    /// 创建实例
    /// </summary>
    [DataContract]
    [Serializable]
    public class SdeConnect
    {
        /// <summary>
        /// 创建实例
        /// </summary>
        public SdeConnect()
        {

        }
        /// <summary>
        /// 创建实例
        /// </summary>
        /// <param name="connectString">sde的连接字符串</param>
        public SdeConnect(string connectString)
            : this()
        {
            this.SetIPropertySet(connectString);
        }
        /// <summary>
        /// 创建实例
        /// </summary>
        /// <param name="server">服务器IP地址</param>
        /// <param name="instance">实例名称</param>
        /// <param name="user">登录用户名称</param>
        /// <param name="password">登录密码</param>
        /// <param name="database">数据库名称</param>
        /// <param name="version">版本</param>
        public SdeConnect(string server, string instance, string user, string password, string database, string version)
            : this()
        {
            this.Server = server.Trim();
            this.Service = instance.Trim();
            this.UserName = user.Trim();
            this.password = password.Trim();
            this.Database = database.Trim();
            this.Version = version.Trim();
            if (string.IsNullOrEmpty(this.Version))
                this.Version = "sde.DEFAULT";
        }
        /// <summary>
        /// 
        /// </summary>
        [DataMember]
        [System.Xml.Serialization.XmlAttribute]
        public string ID { get; set; }
        /// <summary>
        /// 
        /// </summary>
        [DataMember]
        [System.Xml.Serialization.XmlAttribute]
        public string Name { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string Value { get; set; }
        /// <summary>
        /// 服务器IP地址
        /// </summary>
        [DataMember]
        [System.Xml.Serialization.XmlAttribute]
        public string Server
        {
            get;
            set;
        }
        /// <summary>
        /// 实例名称
        /// </summary>
        [DataMember]
        [System.Xml.Serialization.XmlAttribute]
        public string Service
        {
            get;
            set;
        }
        /// <summary>
        /// 登录用户名称
        /// </summary>
        [DataMember]
        [System.Xml.Serialization.XmlAttribute]
        public string UserName
        {
            get;
            set;
        }
        /// <summary>
        /// 登录密码
        /// </summary>
        private string password = "";
        /// <summary>
        /// 登录密码
        /// </summary>
        [DataMember]
        [System.Xml.Serialization.XmlAttribute]
        public string Password
        {
            get { return password; }
            set { password = value; }
        }
        /// <summary>
        /// 数据库名称
        /// </summary>
        [DataMember]
        [System.Xml.Serialization.XmlAttribute]
        public string Database
        {
            get;
            set;
        }
        /// <summary>
        /// 版本
        /// </summary>
        [DataMember]
        [System.Xml.Serialization.XmlAttribute]
        public string Version
        {
            get;
            set;
        }
        /// <summary>
        /// 连接属性信息是否有效
        /// </summary>
        private bool _IsValid = false;
        /// <summary>
        /// 获取连接属性信息是否有效
        /// </summary>
        public bool IsValid
        {
            get
            {
                _IsValid = false;
                if (this.Database == null || this.Service == null || this.Password == null)
                    return _IsValid;
                if (this.Server == null || this.UserName == null || this.Version == "")
                    return _IsValid;
                if (this.Database.Trim() == "" || this.Service.Trim() == "")
                    return _IsValid;
                if (this.Password.Trim() == "" || this.UserName.Trim() == "")
                    return _IsValid;
                if (this.Server.Trim() == "" || this.Version.Trim() == "")
                    return _IsValid;
                _IsValid = true;
                return this._IsValid;
            }
        }
        /// <summary>
        /// 创建sde工作空间实例
        /// </summary>
        /// <param name="connectString">sde的连接字符串</param>
        public void SetIPropertySet(string connectString)
        {
            _IsValid = false;
            if (connectString.Trim() == "")
                return;
            connectString = connectString.Replace(SdeConnect.PrefixConnect.Trim(), "").Trim();
            string[] cnn = connectString.Trim().Split(';');
            if (cnn == null || cnn.Length < 5)
                return;
            foreach (string str in cnn)
            {
                if (str.Trim() == "")
                    continue;
                string[] item = str.Split('=');
                switch (item[0].Trim().ToLower())
                {
                    case "server":
                        this.Server = item[1].Trim();
                        break;
                    case "service":
                        Service = item[1].Trim();
                        break;
                    case "database":
                        Database = item[1].Trim();
                        break;
                    case "version":
                        Version = item[1].Trim();
                        break;
                    case "user":
                        UserName = item[1].Trim();
                        break;
                    case "password":
                        password = item[1].Trim();
                        break;
                }
            }
            if (this.Version.Trim() == "")
                this.Version = "sde.DEFAULT";
            _IsValid = true;
        }
        /// <summary>
        /// 设置工作空间连接属性信息
        /// </summary>
        /// <param name="propertySet"></param>
        public void SetIPropertySet(ESRI.ArcGIS.esriSystem.IPropertySet propertySet)
        {
            this.Server = propertySet.GetProperty("SERVER").ToString();
            this.Service = propertySet.GetProperty("INSTANCE").ToString();
            this.Database = propertySet.GetProperty("DATABASE").ToString();
            this.UserName = propertySet.GetProperty("USER").ToString();
            this.password = propertySet.GetProperty("PASSWORD").ToString();
            this.Version = propertySet.GetProperty("VERSION").ToString();
        }
        /// <summary>
        /// 工作空间连接属性信息
        /// </summary>
        public ESRI.ArcGIS.esriSystem.IPropertySet PropertySet
        {
            get { return GetIPropertySet(); }
        }
        /// <summary>
        /// 获取sde连接名称
        /// </summary>
        public string ConnectName
        {
            get { return this.Server + "_" + this.UserName + ".sde"; }
        }
        /// <summary>
        /// 获取工作空间连接属性信息
        /// </summary>
        /// <returns></returns>
        public ESRI.ArcGIS.esriSystem.IPropertySet GetIPropertySet()
        {
            ESRI.ArcGIS.esriSystem.IPropertySet propertySet = new ESRI.ArcGIS.esriSystem.PropertySet();
            propertySet.SetProperty("SERVER", this.Server);
            propertySet.SetProperty("INSTANCE", this.Service);
            propertySet.SetProperty("DATABASE", this.Database);
            propertySet.SetProperty("USER", this.UserName);
            propertySet.SetProperty("PASSWORD", this.password);
            propertySet.SetProperty("VERSION", this.Version);            
            return propertySet;
        }
        /// <summary>
        /// 转成自定义的标准字符串
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (this.IsValid == false)
                return "";
            string strConn = SdeConnect.PrefixConnect.Trim();
            strConn += "SERVER=" + this.Server.Trim() + ";";
            strConn += "INSTANCE=" + this.Service.Trim() + ";";
            strConn += "DATABASE=" + this.Database.Trim() + ";";
            strConn += "USER=" + this.UserName.Trim() + ";";
            strConn += "PASSWORD=" + this.Password.Trim() + ";";
            strConn += "VERSION=" + this.Version.Trim() + ";";
            return strConn.Trim();
        }
        /// <summary>
        /// 转成标准连接字符串信息
        /// </summary>
        /// <param name="connectstring">工作空间连接信息</param>
        /// <returns></returns>
        public static string CovertConnectString(string connectstring)
        {
            if (connectstring.Trim() == "")
                return "";
            if (connectstring.Trim().StartsWith(SdeConnect.PrefixConnect.Trim(), StringComparison.CurrentCultureIgnoreCase))
                return connectstring.Trim();
            if (connectstring.Trim().Contains(":\\"))
                return connectstring.Trim();
            SdeConnect sde = new SdeConnect(connectstring);
            if (sde.IsValid == false)
                return connectstring;
            connectstring = sde.ToString();
            sde = null;
            return connectstring.Trim();
        }
        /// <summary>
        /// 自定义的连接信息的标准前缀
        /// </summary>
        public static string PrefixConnect
        {
            get { return "connect:"; }
        }

    }
}
